package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;
    @PostMapping
    public R<String>save (@RequestBody DishDto dishDto, HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");

        dishDto.setUpdateUser(employee.getId());
        dishDto.setCreateUser(employee.getId());

        dishService.save(dishDto);

        return R.success("添加成功");


    }
    @GetMapping("/page")
    public R<Page<DishDto>>findByPage(Integer page ,Integer pageSize){
        Page<DishDto> dishDtoPage = dishService.findByPage(page, pageSize);

        return R.success(dishDtoPage);
    }
    @GetMapping("/{id}")
    public R<DishDto>findById(@PathVariable long id){

        DishDto dishDto = dishService.findById(id);

        return R.success(dishDto);

    }
@PutMapping
    public R<String>update(@RequestBody DishDto dishDto,HttpSession session){
    Employee employee = (Employee) session.getAttribute("employee");
    dishDto.setUpdateUser(employee.getId());

    dishService.update(dishDto);

    return R.success("修改成功");

}
//    @DeleteMapping
//        public R<String>deleteById(long id){
//            dishService.deleteById(id);
//        return R.success("删除成功");
//    }
    //批量删除
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long>ids , HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Long opr = employee.getId();
        dishService.deleteByIds(ids,opr);
        return R.success("批量删除成功");
    }


    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId,@RequestParam(defaultValue = "1") Integer status){
        //1.接收类别id
        //2.调用业务根据类别iD查询菜品列表
        List<DishDto> dishList = dishService.findAllByCid(categoryId,status);
        return R.success(dishList);

    }

    @RequestMapping(value = "/status/0",method = RequestMethod.POST)
    public R<String>updateStatusFByIds(@RequestParam List<Long>ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Long opr = employee.getId();
        dishService.updateStatusFByIds(ids,opr);
        return R.success("修改成功!!");

    }

    @RequestMapping(value = "/status/1",method = RequestMethod.POST)
    public R<String>updateStatusTByIds(@RequestParam List<Long>ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Long opr = employee.getId();
        dishService.updateStatusTByIds(ids,opr);
        return R.success("修改成功!!");

    }





}
